package com.guoyh.bilbase.ext

import android.text.Html
import android.widget.TextView
import com.blankj.utilcode.util.SpanUtils
import com.guoyh.baseutils.ext.getColorm
import com.guoyh.baseutils.ext.gone
import com.guoyh.baseutils.ext.inVisible
import com.guoyh.baseutils.ext.visible


/**
 * 作者　: hegaojian
 * 时间　: 2020/11/18
 * 描述　:
 */


/**
 * 获取文本
 */
fun TextView.textString(): String {
    return text.toString()
}

/**
 * 获取去除空字符串的文本
 */
fun TextView.textStringTrim(): String {
    return textString().trim()
}

/**
 * 文本是否为空
 */
fun TextView.isEmpty(): Boolean {
    return textString().isEmpty()
}

/**
 * 去空字符串后文本是否为空
 */
fun TextView.isTrimEmpty(): Boolean {
    return this.textStringTrim().isEmpty()
}

fun TextView.setTextWithSplan(toCreate: (SpanUtils) -> Unit) {
    SpanUtils.with(this).apply {
        toCreate(this)
    }.create()
}

fun SpanUtils.setText(text: String, color: String = "-1") {
    append(text)
    if (color != "-1") {
        setForegroundColor(getColorm(color))
    }
}


fun TextView.setTextWithHtml(des: String) {
    text = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        Html.fromHtml(des, Html.FROM_HTML_MODE_LEGACY)
    } else {
        Html.fromHtml(des)
    }
}

fun TextView.setTextWithHtmlOrGone(des: String) {

    if (des.isNullOrEmpty()) {
        gone()
    } else {
        visible()
        setTextWithHtml(des)
    }

}


fun TextView.setTextWithHtmlOrInVisible(des: String) {

    if (des.isNullOrEmpty()) {
        inVisible()
    } else {
        visible()
        setTextWithHtml(des)

    }

}

fun TextView.setTextOrGone(text: String) {
    if (text.isNullOrEmpty()) {
        gone()
    } else {
        visible()
        setText(text)
    }
}


fun TextView.setTextOrInVisible(text: String) {
    if (text.isNullOrEmpty()) {
        inVisible()
    } else {
        visible()
        setText(text)
    }
}


